<template>
    <div>
        <hs-breadcrumb :breadcrumb="breadcrumb"></hs-breadcrumb>
        <Card :bordered="false" :style="{marginBottom:'10px'}">
            <div class="hs-screen-row">
                <Input v-model="params.username" placeholder="账号" clearable style="width: 200px"/>
            </div>
            <div class="hs-screen-row">
                <Select v-model="params.roleId" placeholder="权限组" clearable style="width:200px">
                    <Option value="">全部</Option>
                    <Option v-for="item in roleList" :value="item.adminRoleId" :key="item.adminRoleId">{{ item.roleName
                        }}
                    </Option>
                </Select>
            </div>
            <div class="hs-screen-row">
                <Select v-model="params.isDisable" placeholder="状态" clearable style="width:200px">
                    <Option value="">全部</Option>
                    <Option value="0">已启用</Option>
                    <Option value="1">已禁用</Option>
                </Select>
            </div>
            <div class="hs-screen-row">
                <Button type="primary" @click="findAdminUserList">查询</Button>
            </div>
            <div class="hs-screen-row">
                <Button @click="resetScreen">重置</Button>
            </div>
        </Card>

        <Card :bordered="false" :style="{marginBottom:'10px'}">
            <!--标题-->
            <div slot="title">
                <div class="hs-screen-box">
                    <div>管理员列表</div>
                    <div>
                        <ButtonGroup>
                            <Button @click="findAdminUserList">
                                <Icon type="md-refresh" size="15"/>
                            </Button>
                            <Button @click="showAdminOpe(2)">新增管理员</Button>
                        </ButtonGroup>
                    </div>
                </div>
            </div>
            <!--表格数据-->
            <Table :columns="columns" :data="data" size="small" :stripe="true" :loading="isTableLoading">

                <template slot-scope="{ row, index }" slot="isDisable">
                    <Tag color="green" v-if="row.isDisable === 0" size="default">启用</Tag>
                    <Tag color="volcano" v-if="row.isDisable === 1" size="default">禁用</Tag>
                </template>

                <template slot-scope="{ row }" slot="action">
                    <Button type="success" size="small" style="margin-right: 5px" @click="showAdminOpe(1, row)">查看
                    </Button>
                    <Button type="primary" size="small" style="margin-right: 5px" @click="showAdminOpe(3, row)">编辑
                    </Button>

                    <Dropdown transfer trigger="click">
                        <Button size="small">
                            <Icon type="ios-more" size="22"/>
                        </Button>
                        <DropdownMenu slot="list">
                            <DropdownItem @click.native="isResetPassword(row)">重置密码</DropdownItem>
                            <DropdownItem v-if="row.isDisable === 0" @click.native="isDisableUser(row)">禁用
                            </DropdownItem>
                            <DropdownItem v-if="row.isDisable === 1" @click.native="isDisableUser(row)">启用
                            </DropdownItem>
                            <DropdownItem @click.native="isDelUser(row)">删除</DropdownItem>
                        </DropdownMenu>
                    </Dropdown>
                </template>
            </Table>
            <!--分页-->
            <div class="hs-page">
                <Page :total="total" show-sizer show-total transfer @on-change="changePage"
                      @on-page-size-change="changePageSize"/>
            </div>
        </Card>

        <!--显示模态框-->
        <admin-ope ref="adminOpe" @changeDate="findAdminUserList" :roleList="roleList"></admin-ope>
    </div>
</template>

<script>
    import hsBreadcrumb from '@/components/hs-breadcrumb';
    import adminOpe from './components/admin-ope';

    export default {
        name: "admin",
        data() {
            return {
                total: 0,
                isTableLoading: false,
                roleList: [],
                // 参数
                params: {
                    page: 1,
                    rows: 10,
                    username: '',
                    roleId: '',
                    isDisable: ''
                },
                // 表格列
                columns: [
                    {
                        type: 'index',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: 'ID',
                        minWidth: 120,
                        key: 'adminId'
                    },
                    {
                        title: '账号',
                        minWidth: 120,
                        key: 'username'
                    },
                    {
                        title: '姓名',
                        minWidth: 120,
                        key: 'realName'
                    },
                    {
                        title: '权限组',
                        minWidth: 120,
                        key: 'roleName'
                    },
                    {
                        title: '状态',
                        slot: 'isDisable',
                        align: 'center',
                        width: 120
                    },
                    {
                        title: '创建时间',
                        key: 'createTime',
                        width: 150
                    },
                    {
                        title: '操作',
                        slot: 'action',
                        width: 180,
                        align: 'center'
                    }
                ],
                // 表格数据
                data: [],
                // 面包屑
                breadcrumb: [
                    {name: '首页', url: '', icon: 'ios-home-outline', size: 18},
                    {name: '系统管理', url: '', icon: '', size: 0},
                    {name: '管理员列表', url: '', icon: '', size: 0}
                ]
            }
        },
        created() {
            // 管理员-列表查询
            this.findAdminUserList();
            // 管理员-查询管理员权限组，筛选条件使用
            this.findRoleList();
        },
        methods: {
            // 管理员-列表查询
            findAdminUserList() {
                this.isTableLoading = true;
                this.axios.post('/adminUser/findAdminUserList', this.params).then(res => {
                    
                    this.isTableLoading = false;
                    if (res.status == 200) {
                        this.total = res.data.total;
                        this.data = res.data.rows;
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '查询管理员列表接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 管理员-查询管理员权限组，筛选条件使用
            findRoleList() {
                this.axios.post('/adminRole/findRoleList', {
                    page: 1,
                    rows: 100000
                }).then(res => {
                    
                    if (res.status == 200) {
                        this.roleList = res.data.rows;
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '查询管理员权限组接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 是否重置密码
            isResetPassword(row) {
                this.$Modal.confirm({
                    title: `是否重置管理员${row.username}的密码？`,
                    onOk: () => {
                        // this.$Message.info('您点击了确定.');
                        this.resetPassword(row);
                    },
                    onCancel: () => {
                        this.$Message.info('您点击了取消.');
                    }
                });
            },
            // 是否删除管理员
            isDelUser(row) {
                // console.log(row)
                this.$Modal.confirm({
                    title: `是否删除管理员${row.username}？`,
                    onOk: () => {
                        // this.$Message.info('您点击了确定.');
                        this.deleteAdmin(row);
                    },
                    onCancel: () => {
                        this.$Message.info('您点击了取消.');
                    }
                });
            },
            // 是否禁用管理员
            isDisableUser(row) {
                // console.log(row)
                this.$Modal.confirm({
                    title: `是否${row.isDisable ? '开启' : '禁用'}管理员${row.username}？`,
                    onOk: () => {
                        this.disableAdmin(row);
                    },
                    onCancel: () => {
                        this.$Message.info('您点击了取消.');
                    }
                });
            },
            // 重置管理员密码
            resetPassword(row) {
                this.$Spin.show();
                this.axios.post('/adminUser/resetPassword', {
                    adminId: row.adminId
                }).then(res => {
                    
                    setTimeout(() => {
                        this.$Spin.hide();
                    }, 300);
                    if (res.status == 200) {
                        this.$Message.success('操作成功.');
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '重置管理员密码接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 删除管理员
            deleteAdmin(row) {
                this.$Spin.show();
                this.axios.post('/adminUser/deleteAdmin', {
                    adminId: row.adminId
                }).then(res => {
                    
                    setTimeout(() => {
                        this.$Spin.hide();
                    }, 300);
                    if (res.status == 200) {
                        this.$Message.success('操作成功.');
                        this.findAdminUserList();
                    }
                    if (res.status == 201) {
                        // this.$Message.error(res.msg);
                        this.$Notice.error({
                            title: '删除管理员失败',
                            desc: res.msg
                        });
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '删除管理员接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 禁用或开启管理员
            disableAdmin(row) {
                this.$Spin.show();
                this.axios.post('/adminUser/disableAdmin', {
                    adminId: row.adminId,
                    isDisable: row.isDisable ? 0 : 1
                }).then(res => {
                    
                    setTimeout(() => {
                        this.$Spin.hide();
                    }, 300);
                    if (res.status == 200) {
                        this.$Message.success('操作成功.');
                        this.findAdminUserList();
                    }
                    if (res.status == 500) {
                        this.$Notice.error({
                            title: '服务器内部错误',
                            desc: '禁用管理员接口报错',
                            duration: 0
                        });
                    }
                })
            },
            // 显示操作模态框
            showAdminOpe(type, row) {
                /**
                 * type判断
                 * 1.查看 2.新增 3.编辑
                 */
                this.$refs.adminOpe.show(type, row);
            },
            // 重置筛选条件
            resetScreen() {
                this.params.username = '';
                this.params.roleId = '';
                this.params.isDisable = '';
            },
            // 改变页数
            changePage(e) {
                this.params.page = e;
                this.findAdminUserList();
            },
            // 改变页容量
            changePageSize(e) {
                this.params.rows = e;
                this.findAdminUserList();
            },
        },
        components: {
            hsBreadcrumb,
            adminOpe
        }
    }
</script>

<style scoped>

</style>